using Blackbird.Common.Exceptions;
using Blackbird.Core;
using Blackbird.Core.Editable.Frames;
using Blackbird.Core.Entities.Sprites;
using Microsoft.Xna.Framework.Content.Pipeline;

namespace Blackbird.Pipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to apply custom processing to content data, converting an object of
    /// type TInput to TOutput. The input and output types may be the same if
    /// the processor wishes to alter data without changing its type.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    ///
    /// TODO: change the ContentProcessor attribute to specify the correct
    /// display name for this processor.
    /// </summary>
    [ContentProcessor(DisplayName = "Blackbird.Pipeline.FrameProcessor")]
    public class FrameProcessor : ContentProcessor<EditableFrame, Frame>
    {
        public override Frame Process(EditableFrame input, ContentProcessorContext context)
        {
            
            Frame frame = null;
            if (input != null)
            {
                
                var textureEntry = new TextureEntry(input.GetBitmapBytes(), TexturePool.Instance.Device, input.BitmapSource.PixelWidth,
                                                    input.BitmapSource.PixelHeight);
                int textureId = TexturePool.Instance.RegisterTexture(textureEntry);
                
                frame = new Frame
                            {
                                Index = input.Index,
                                Height = input.BitmapSource.PixelHeight,
                                Width = input.BitmapSource.PixelWidth,
                                TextureId = textureId
                            };
            }
            return frame;
        }

    }
}